"use client";
// ^ this file needs the "use client" pragma
import { setVerbosity } from "ts-invariant";
import { ApolloLink, HttpLink } from "@apollo/client";
import { SchemaLink } from '@apollo/client/link/schema';
import {
  ApolloNextAppProvider,
  ApolloClient,
  InMemoryCache,
  SSRMultipartLink,
} from "@apollo/experimental-nextjs-app-support";

setVerbosity("debug");
// have a function to create a client for you
function makeClient() {
  const httpLink = new HttpLink({
    // this needs to be an absolute url, as relative urls cannot be used in SSR
    uri: "https://zion-app.functorz.com/zero/0lqQ0rOl9zY/api/graphql-v2",
    // you can disable result caching here if you want to
    // (this does not work if you are rendering your page with `export const dynamic = "force-static"`)
    fetchOptions: { cache: "no-store" },
    headers: {
      Authorization: "Bearer eyJhbGciOiJIUzI1NiJ9.eyJkZWZhdWx0Um9sZSI6ImFkbWluIiwiWkVST19VU0VSX0lEIjoiMTAwOTk5OTk5OTk5OTk5OSIsInJvbGVzIjpbImFkbWluIl0sInplcm8iOnt9LCJoYXN1cmFfY2xhaW1zIjp7IngtaGFzdXJhLWRlZmF1bHQtcm9sZSI6ImFkbWluIiwieC1oYXN1cmEtYWxsb3dlZC1yb2xlcyI6WyJhZG1pbiJdLCJ4LWhhc3VyYS11c2VyLWlkIjoiMTAwOTk5OTk5OTk5OTk5OSJ9fQ.ROqmuXGuZ_WrgVIRG_LieZdw2gD2emThjMyLTZ8MQuo",
    }
    // you can override the default `fetchOptions` on a per query basis
    // via the `context` property on the options passed as a second argument
    // to an Apollo Client data fetching hook, e.g.:
    // const { data } = useSuspenseQuery(MY_QUERY, { context: { fetchOptions: { cache: "force-cache" }}});
  });

  // use the `ApolloClient` from "@apollo/experimental-nextjs-app-support"
  return new ApolloClient({
    ssrMode: true,
    // use the `InMemoryCache` from "@apollo/experimental-nextjs-app-support"
    cache: new InMemoryCache(),
    link: httpLink,
  });
}

// you need to create a component to wrap your app in
export function ApolloWrapper({ children }: React.PropsWithChildren) {
  return (
    <ApolloNextAppProvider makeClient={makeClient}>
      {children}
    </ApolloNextAppProvider>
  );
}

